﻿using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 类名 : 自定义数字对应美术图片的逻辑实现 <br/>
    /// 作者 : Canyon / 龚阳辉 <br/>
    /// 日期 : 2024-06-25 17:12 <br/>
    /// 功能 : 展示布局需要结合Layout，这里不做布局处理
    /// </summary>
    [DisallowMultipleComponent]
    public class ExArtSpriteNum : ExMono
    {
        protected ExArtSpriteNum() : base()
        {
        }

        [SerializeField] ArtSpriteNumAsset asset;
        [SerializeField] bool isNativeSize = false;
        [SerializeField] bool isChangeSelf = false;
        // 逻辑变量
        [HideInInspector] List<ExImage> imgs = null;
        [HideInInspector] ExImage imgSelf = null;

        private string _text = null;
        public string text
        {
            get { return this._text; }
            set
            {
                if (string.Equals(this._text, value))
                    return;
                this._text = value;
                this.OnTextChanged();
            }
        }

        void OnTextChanged()
        {
            string _text = null;
            int cnt = 0;
            if (!string.IsNullOrEmpty(this.text))
            {
                _text = this.text.Trim();
                cnt = _text.Length;
            }

            if (this.isChangeSelf)
            {
                if (this.imgSelf == null)
                    this.imgSelf = this.gameObject.GetOrAdd<ExImage>();
                int val = -1;
                int.TryParse(_text, out val);
                this.imgSelf.sprite = this.asset[val];
                if (this.isNativeSize && this.imgSelf.sprite != null)
                    this.imgSelf.SetNativeSize();
                return;
            }

            Transform trsfRoot = this.transform;
            ExImage imgCurr = null;
            if (this.imgs == null)
            {
                int cntChild = trsfRoot.childCount;
                this.imgs = new();
                for (int i = 0; i < cntChild; i++)
                {
                    imgCurr = this.transform.GetChild(i).gameObject.GetOrAdd<ExImage>();
                    this.imgs.Add(imgCurr);
                }
            }

            int cntImgs = this.imgs.Count;
            int ikey = -1;
            ExImage _imgInstant = null;
            for (int i = 0; i < cnt; i++)
            {
                if (_imgInstant == null && this.imgs.Count > 0)
                    _imgInstant = this.imgs[0];

                if (i < cntImgs)
                {
                    imgCurr = this.imgs[i];
                }
                else if (_imgInstant != null)
                {
                    imgCurr = GameObject.Instantiate<ExImage>(_imgInstant, trsfRoot, false);
                    imgCurr.name = $"index-{i}";
                    this.imgs.Add(imgCurr);
                }
                else
                {
                    Transform trsfCurr = new GameObject($"Index-{i}", typeof(ExImage)).transform;
                    trsfCurr.SetParent(trsfRoot, false);
                    imgCurr = trsfCurr.GetComponent<ExImage>();
                    this.imgs.Add(imgCurr);
                }
                ikey = -1;
                int.TryParse(_text.Substring(i, 1), out ikey);
                imgCurr.sprite = this.asset[ikey];
                imgCurr.SetActive(true);
                if (this.isNativeSize && imgCurr.sprite != null)
                    imgCurr.SetNativeSize();
            }

            for (int i = cnt; i < cntImgs; i++)
            {
                imgCurr = this.imgs[i];
                imgCurr.SetActive(false);
            }
        }

        public void SetInt(int val)
        {
            this.text = val.ToString();
        }

        public void SetText(string text)
        {
            this.text = text;
        }
    }
}
